const app = new Vue({
    el: '#app',
    data: {
        books: [
            {
                id: 1,
                name: '方法导论',
                date: '2006-9',
                price: 20,
                count: 1

            },
            {
                id: 2,
                name: '重构的艺术',
                date: '2016-9',
                price: 30,
                count: 1

            },

        ]
    },
    methods:{
        //过滤器代替方法
        // getFinalPrice(price){
        //     return '￥'+price.toFixed(2)
        // }
        increment(index){
            this.books[index].count++
        },
        decrement(index){
            this.books[index].count--
        },
        removeHandler(index){
            this.books.splice(index,1)
        }
    },
    //计算属性
    computed:{
        totalPrice(){
            let totalPrice=0;
            // for(let book of this.books){
            //     totalPrice+=(book.price*book.count)
            // }
            // return totalPrice;
            totalPrice=this.books.reduce((pre,book)=>pre+book.price*book.count,0)
            return totalPrice;
        }
    },
    filters:{
        getFinalPrice(price){
            return '￥'+price.toFixed(2)
        }
    }
})